/*=========================================================================

Program:   Medical Imaging & Interaction Toolkit
Language:  C++
Date:      $Date: 2010-03-15 18:12:36 +0800 (周一, 15 三月 2010) $
Version:   $Revision: 21745 $

Copyright (c) German Cancer Research Center, Division of Medical and
Biological Informatics. All rights reserved.
See MITKCopyright.txt or http://www.mitk.org/copyright.html for details.

This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the above copyright notices for more information.

=========================================================================*/


#include "WxObjectOrientationMarkerMapperOverlay.h"

#include "mitkBaseRenderer.h"
#include "mitkPlaneGeometry.h"
#include "mitkColorProperty.h"
#include "mitkProperties.h"

#include <vtkRenderer.h>

#include "mitkGL.h"

mitk::WxObjectOrientationMarkerMapperOverlay::WxObjectOrientationMarkerMapperOverlay()
{
}

mitk::WxObjectOrientationMarkerMapperOverlay::~WxObjectOrientationMarkerMapperOverlay()
{
}


void mitk::WxObjectOrientationMarkerMapperOverlay::Paint(mitk::BaseRenderer * renderer)
{
    if(IsVisible(renderer)==false) return;

    bool updateNeccesary=true;

    if (updateNeccesary)
    {
        ApplyProperties(renderer);

        float lineWidth = 5.0;
        float lineLength =40.0;
        mitk::Color coordinateXDiaplayColor;coordinateXDiaplayColor.Set(1,0,0);
        mitk::Color coordinateYDiaplayColor;coordinateYDiaplayColor.Set(0,1,0);
        mitk::Color coordinateZDiaplayColor;coordinateZDiaplayColor.Set(0,0,1);
        if (dynamic_cast<mitk::FloatProperty *>(this->GetDataNode()->GetProperty("CoordinateDiaplayWidth")) != NULL)
            lineWidth = dynamic_cast<mitk::FloatProperty*>(this->GetDataNode()->GetProperty("CoordinateDiaplayWidth"))->GetValue();
        if (dynamic_cast<mitk::FloatProperty *>(this->GetDataNode()->GetProperty("CoordinateDiaplayLength")) != NULL)
            lineLength = dynamic_cast<mitk::FloatProperty*>(this->GetDataNode()->GetProperty("CoordinateDiaplayLength"))->GetValue();

        if (dynamic_cast<mitk::ColorProperty *>(this->GetDataNode()->GetProperty("CoordinateXDiaplayColor")) != NULL)
            coordinateXDiaplayColor = dynamic_cast<mitk::ColorProperty*>(this->GetDataNode()->GetProperty("CoordinateXDiaplayColor"))->GetColor();
        if (dynamic_cast<mitk::ColorProperty *>(this->GetDataNode()->GetProperty("CoordinateYDiaplayColor")) != NULL)
            coordinateYDiaplayColor = dynamic_cast<mitk::ColorProperty*>(this->GetDataNode()->GetProperty("CoordinateYDiaplayColor"))->GetColor();
        if (dynamic_cast<mitk::ColorProperty *>(this->GetDataNode()->GetProperty("CoordinateZDiaplayColor")) != NULL)
            coordinateZDiaplayColor = dynamic_cast<mitk::ColorProperty*>(this->GetDataNode()->GetProperty("CoordinateZDiaplayColor"))->GetColor();

        mitk::Point3D ori=this->GetData()->GetUpdatedGeometry()->GetOrigin();
        mitk::Point3D coordinateXPoint=ori+this->GetData()->GetUpdatedGeometry()->GetAxisVector(0);
        mitk::Point3D coordinateYPoint=ori+this->GetData()->GetUpdatedGeometry()->GetAxisVector(1);
        mitk::Point3D coordinateZPoint=ori+this->GetData()->GetUpdatedGeometry()->GetAxisVector(2);

        double vtkOrigin2d[3],vtkCordX2d[3],vtkCordY2d[3],vtkCordZ2d[3];
        vtkRenderer* vtkRen=renderer->GetVtkRenderer();
        vtkRen->SetWorldPoint(ori[0],ori[1],ori[2],1.0);
        vtkRen->WorldToDisplay();
        vtkRen->GetDisplayPoint(vtkOrigin2d);
        vtkRen->SetWorldPoint(coordinateXPoint[0],coordinateXPoint[1],coordinateXPoint[2],1.0);
        vtkRen->WorldToDisplay();
        vtkRen->GetDisplayPoint(vtkCordX2d);
        vtkRen->SetWorldPoint(coordinateYPoint[0],coordinateYPoint[1],coordinateYPoint[2],1.0);
        vtkRen->WorldToDisplay();
        vtkRen->GetDisplayPoint(vtkCordY2d);
        vtkRen->SetWorldPoint(coordinateZPoint[0],coordinateZPoint[1],coordinateZPoint[2],1.0);
        vtkRen->WorldToDisplay();
        vtkRen->GetDisplayPoint(vtkCordZ2d);
        mitk::Vector2D cordX2d,cordY2d,cordZ2d;
        cordX2d[0]=vtkCordX2d[0]-vtkOrigin2d[0]; cordX2d[1]=vtkCordX2d[1]-vtkOrigin2d[1];
        cordY2d[0]=vtkCordY2d[0]-vtkOrigin2d[0]; cordY2d[1]=vtkCordY2d[1]-vtkOrigin2d[1];
        cordZ2d[0]=vtkCordZ2d[0]-vtkOrigin2d[0]; cordZ2d[1]=vtkCordZ2d[1]-vtkOrigin2d[1];
        cordX2d.Normalize();cordX2d=cordX2d*lineLength;
        cordY2d.Normalize();cordY2d=cordY2d*lineLength;
        cordZ2d.Normalize();cordZ2d=cordZ2d*lineLength;


        glLineWidth(lineWidth);
        glBegin(GL_LINES);

        glColor4f (coordinateXDiaplayColor[0],coordinateXDiaplayColor[1],coordinateXDiaplayColor[2],1.0);
        glVertex2f(vtkOrigin2d[0],vtkOrigin2d[1]);
        glVertex2f(vtkOrigin2d[0]+cordX2d[0],vtkOrigin2d[1]+cordX2d[1]);

        glColor4f (coordinateYDiaplayColor[0],coordinateYDiaplayColor[1],coordinateYDiaplayColor[2],1.0);
        glVertex2f(vtkOrigin2d[0],vtkOrigin2d[1]);
        glVertex2f(vtkOrigin2d[0]+cordY2d[0],vtkOrigin2d[1]+cordY2d[1]);

        glColor4f (coordinateZDiaplayColor[0],coordinateZDiaplayColor[1],coordinateZDiaplayColor[2],1.0);
        glVertex2f(vtkOrigin2d[0],vtkOrigin2d[1]);
        glVertex2f(vtkOrigin2d[0]+cordZ2d[0],vtkOrigin2d[1]+cordZ2d[1]);

        glEnd ();
        glLineWidth(1.0);
    }
}
void mitk::WxObjectOrientationMarkerMapperOverlay::SetDefaultProperties(mitk::DataNode* node, mitk::BaseRenderer* renderer, bool overwrite)
{
  node->AddProperty( "CoordinateDiaplayWidth", mitk::FloatProperty::New(3.0), renderer, overwrite );
  node->AddProperty( "CoordinateDiaplayLength", mitk::FloatProperty::New(30.0), renderer, overwrite );
  node->AddProperty( "CoordinateXDiaplayColor", mitk::ColorProperty::New(1.0f,0.0f,0.0f), renderer, overwrite );
  node->AddProperty( "CoordinateYDiaplayColor", mitk::ColorProperty::New(0.0f,1.0f,0.0f), renderer, overwrite );
  node->AddProperty( "CoordinateZDiaplayColor", mitk::ColorProperty::New(0.0f,0.0f,1.0f), renderer, overwrite );

  Superclass::SetDefaultProperties(node, renderer, overwrite);
}

